package com.qdxwx.manage.controller.manager;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.manager.ManagerService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.manager.Manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户信息
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/manager")
public class ManagerController extends BaseController {

    @Autowired
    private ManagerService managerService;

    @GetMapping("/list.html")
    public String list() {
        return "manager/manager-list";
    }


    @PostMapping("/list.json")
    @Permissions("manager:list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String searchKey = request.getParameter("searchKey");
        List<Manager> list = new ArrayList<>();
        Manager manager = new Manager();
        if (TKString.isMobile(searchKey)) {
            manager.setMobile(searchKey);
        } else if (TKConvert.toInteger(searchKey) > 0) {
            list.add(managerService.get(TKConvert.toInteger(searchKey)));
            return TableDataInfo.success(list,1);
        } else {
            manager.setManagerName(searchKey);
        }
        list = managerService.getList(manager, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(managerService.count(manager));
        return rspData;
    }


    /**
     * 用户状态修改
     */
    @Log(title = "禁用管理员", businessType = BusinessType.UPDATE)
    @Permissions("manager:edit")
    @PostMapping("/disabled.json")
    @ResponseBody
    public AjaxResult disabled(HttpServletRequest request) {
        int managerId = TKConvert.toInteger(request.getParameter("managerId"));
        int isDisabled = TKConvert.toInteger(request.getParameter("isDisabled"));
        Manager manager = new Manager();
        manager.setManagerId(managerId);
        manager.setIsDisabled(isDisabled >= 1 ? 1 : 0);
        managerService.checkUserAllowed(manager);
        return toAjax(managerService.updateDisabled(manager));
    }
}